﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Ckknight.StateMachine.SilverlightTestApp
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            _stateMachine = StateMachineBuilder.Create<AppState, Trigger>()
                .Configure(AppState.Starting)
                    .Permit(Trigger.Initialized, AppState.Alpha)
                    .End()
                .Configure(AppState.Alpha)
                    .OnEntry(() =>
                    {
                        button1.IsEnabled = false;
                        button2.IsEnabled = true;
                        textBlock1.Text = "Alpha";
                    })
                    .Permit(Trigger.Button2Clicked, AppState.Bravo)
                    .End()
                .Configure(AppState.Bravo)
                    .Permit(Trigger.Button1Clicked, AppState.Alpha)
                    .OnEntry(() =>
                    {
                        button1.IsEnabled = true;
                        button2.IsEnabled = false;
                        textBlock1.Text = "Bravo";
                    })
                    .End()
                .CreateFactory()
                .Generate(AppState.Starting);

            InitializeComponent();

            _stateMachine.Fire(Trigger.Initialized);
        }

        enum AppState
        {
            Starting,
            Alpha,
            Bravo,
        }

        enum Trigger
        {
            Initialized,
            Button1Clicked,
            Button2Clicked,
        }

        private readonly StateMachine<AppState, Trigger, object> _stateMachine;

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            _stateMachine.Fire(Trigger.Button1Clicked);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            _stateMachine.Fire(Trigger.Button2Clicked);
        }
    }
}
